package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysRole;
import com.ktjiaoyu.mapper.SysRoleMapper;
import com.ktjiaoyu.service.SysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    private static final Logger LOGGER = LoggerFactory.getLogger(SysRoleServiceImpl.class);
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public List<SysRole> findAll() throws Exception {
        try {
            return sysRoleMapper.getAll();
        } catch (Exception e) {
            LOGGER.error("执行SysRoleServiceImpl.findAll()方法异常.", e);
            throw e;
        }
    }

    @Override
    public boolean codeExist(String code) throws Exception {
        try {
            //调用service层方法验证角色code是否唯一
            SysRole sysRole = sysRoleMapper.checkRoleCodeUnique(code);
            if (sysRole != null) {
                return false;
            }
        } catch (Exception e) {
            LOGGER.error("执行SysRoleServiceImpl.save()方法异常.", e);
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public boolean save(SysRole sysRole) {
        try {
            if (sysRole.getId() == null) {
                return sysRoleMapper.insert(sysRole) > 0;
            } else {
               return sysRoleMapper.update(sysRole)>0;
            }
        } catch (Exception e) {
            LOGGER.error("执行SysRoleServiceImpl.save()方法异常.", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public SysRole findById(Long id) {
        try {
            return sysRoleMapper.findById(id);
        }catch (Exception e){
            LOGGER.error("执行SysRoleServiceImpl.findById()方法异常.", e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean del(Long id) {
        return sysRoleMapper.delete(id)>0;
    }
}
